<template>
	<om-formsub-outer :title="title" :index="index" @save="save" @delete="del">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		formValidation,
		changeEditor,
		configSelectData,
		hasEditor
	} from '../../../static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js'
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				formdata: {},
				
				autoformdata: [],
				cachename: '',
				index: null,
				datas: [],
				order: 0,
				bearOff3CacheKey: "obj-m-proj-bearoff3-cache",
				evaOff3CacheKey: 'obj-m-proj-evaoff3-cache',
				defaultDataCache:'obj-m-proj-evaoff3-work-user-defaultData-cache',
				score: '',
				title: '',
				minDate: '',
				canEditorEvaMultiOffId:false,
				busCode:'',
				canEdit:true
			})
		},
		methods: {
			clearEvaMultiOffId(){
				let _that = this
				_that.$refs.autoform.clear("title10","evaMultiOffId")
				uni.removeStorage({
					key:_that.defaultDataCache
				})
			},
			clearReportEvaCode(){
				let _that = this
				_that.$refs.autoform.clear("title10","reportEvaCode")
				_that.$refs.autoform.clear("title10","evaMultiOffId")
				_that.$refs.autoform.clear("title10","maxScore")
				_that.$refs.autoform.changeEditor("title10","evaMultiOffId",'type','labelbox')
				_that.$refs.autoform.changeEditor("title10","maxScore",'type','labelbox')
			},
			getMultiEvaUserResponse(userIdPosId){
				let _that = this
				_that.om.request(
					"workEvaluateConfig/getMultiEvaUserResponse",
					{
						busCode:_that.busCode,
						userIdPosId: userIdPosId
					}
				).then(res=>{
					if(_that.canEdit){
						let objArr = []
						let ids = res.value.evaMultiOffId.split(',')
						let names = res.value.evaMultiOffName.split(',')
						let posNames = res.value.evaMultiPosName.split(',')
						for(let index in ids){
							let obj = {
								checked: true,
								id: null,
								instId: null,
								instName: '',
								isUser: true,
								posId: null,
								posInstName:'',
								posName: '',
								username: ''
							}
							let userPosId = ids[index].split('/')
							obj.id = userPosId[0]
							obj.posId = userPosId[1]
							obj.username = names[index]
							obj.posName = posNames[index]
							objArr.push(obj)
						}
						_that.$refs.autoform.formdata.evaMultiPosName = res.value.evaMultiPosName
						uni.setStorage({
							key:_that.evaMultiOffDefaultDataCache,
							data:objArr
						})
					}
					_that.$refs.autoform.formdata.evaMultiOffId = res.value.evaMultiOffId
					_that.$refs.autoform.formdata.evaMultiOffName = res.value.evaMultiOffName
				})
			},
			onSelectReportEva(item) {
				let _that = this
				if (item.code == '39758') {
					if(_that.canEdit){
						_that.$refs.autoform.changeEditor('title10', "evaMultiOffId", "type", 'linkbox')
						_that.$refs.autoform.changeEditor('title10', "evaMultiOffId",{
							"link":'../../public/searchUser/searchUser',
							"linkParam":{
								other:JSON.stringify({
								}),
								defaultDataCache:_that.defaultDataCache,
								isUserPos:true,
								isNew:true,
								cachename:_that.evaOff3CacheKey
							}
						})
						_that.$refs.autoform.changeEditor('title10', "maxScore", "type", 'numberbox')
					}
					if(_that.$refs.autoform.formdata.dutyOffId){
						_that.getMultiEvaUserResponse(_that.$refs.autoform.formdata.dutyOffId)
					}
					_that.$refs.autoform.formdata.maxScore = 10
				} else {
					_that.$refs.autoform.clear('title10', "evaMultiOffId")
					_that.$refs.autoform.changeEditor('title10', "evaMultiOffId", "type", 'labelbox')
					_that.$refs.autoform.clear('title10', "maxScore")
					_that.$refs.autoform.changeEditor('title10', "maxScore", "type", 'labelbox')
				} 
			},
			onSelectPlanEnd(date) {
				let _that = this
				if (moment(_that.minDate).isSameOrAfter(date)) {
					_that.om.toast('请选择' + _that.minDate + '之后的日期')
					_that.$refs.autoform.clear("title10", 'planEnd')
				}
			},
			clearDutyOff() {
				let _that = this
				_that.$refs.autoform.clear("title10", "dutyOffId")
				uni.removeStorage({
					key: 'searchUserDefaultList'
				})
			},
			onInputScore(value) {
				let _that = this 
				if (Number(value) > Number(_that.score)) {
					_that.$refs.autoform.formdata.maxScore = Number(_that.score)
				}
			},
			save() {
				let _that = this
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata)
				if (!checkRes) {
					if (this.index != null) {
						Object.assign(this.datas[this.index], this.formdata)
					} else {
						this.datas.push(this.formdata)
					}
					uni.setStorage({
						key: this.cachename,
						data: this.datas,
					})
					uni.navigateBack()
				} else {
					_that.om.toast(checkRes);
				}
			},
			del() {
				if (this.index != null) {
					this.datas.splice(this.index, 1)
				}
				uni.setStorage({
					key: this.cachename,
					data: this.datas
				})
				uni.navigateBack()
			},
			onSelectDutyOffId(userId) {
				var editor = this.$refs.autoform.getEditor("title10", "evaMultiOffId");
				if (editor && editor.linkParam) {
					editor.linkParam.other = JSON.stringify({
						noUserIdStr: userId
					})
				}
			},
			initConfig(e) {
				let _that = this
				let pro1 = _that.om.request("dataDict/getIntByCode", {
					code: 'DD021017003'
				})
				let pro2 = _that.om.request("/orgInstPosUserOfficial/findListByUserIdOrgId")
				let pro3 = _that.om.request('/tsysdict/getListByCode', {
					code: '39757'
				})
				let pro4 = _that.om.request('dictZero/findBooleanDtoList')
				let pro5 = _that.om.request('dictSource/loadCombo',{
					sourceId:'289',
					categoryId:'8566'
				 })
				let pro6 = _that.om.request('dictSource/loadCombo',{
					sourceId:'283',
					categoryId:'6116'
				})
				
				Promise.all([pro1, pro2, pro3,pro4,pro5,pro6]).then(res => {
					_that.score = res[0].value
					_that.$refs.autoform.changeEditor("title10", "maxScore", "onSelect", "onInputScore")
					_that.$refs.autoform.changeEditor('title10', "dutyOffId", {
						"linkParam": {
							url:'orgInstPosUserOfficial/findListByUserIdOrgId',
							namefield:'userPosInstName',
							initload:true,
							key2:'posId',
							key:'userId',
							searchfield: "q",
							cachename: _that.bearOff3CacheKey,
						}
					})
					_that.$refs.autoform.changeEditor('title10', 'reportEvaCode', 'data', configSelectData(res[2],
						'code', 'dicname'))
					if (hasEditor("title10", "reportEvaCode", _that.autoformdata) && !_that.formdata
						.reportEvaCode) {
						let selectIndex  = ''
						for(let index in res[2]){
							if(res[2][index] = '39759'){
								selectIndex = index
							}
						}
						_that.$refs.autoform.initSelectVal("title10", "reportEvaCode", selectIndex)
					} else {
						_that.$refs.autoform.initSelectIndex("title10", "reportEvaCode")
					}
					_that.$refs.autoform.changeEditor("title10","keyItemFlag",'data',configSelectData(res[3],'value','name'))
					_that.$refs.autoform.initSelectIndex("title10","keyItemFlag")
					_that.$refs.autoform.changeEditor("title10","importanceCode",'data',configSelectData(res[4],'code','text'))
					_that.$refs.autoform.initSelectIndex("title10","importanceCode")
					_that.$refs.autoform.changeEditor("title10","workCateCode",'data',configSelectData(res[5],'code','text'))
					_that.$refs.autoform.initSelectIndex("title10","workCateCode")
				})
			}
		},
		onUnload() {
			let _that = this
			uni.removeStorage({
				key:_that.defaultDataCache
			})
			uni.removeStorage({
				key: "searchUserDefaultList"
			})
		},
		onLoad(e) {
			let _that = this
			let arr = [
				{
					url:"dataDict/getIntByCode",
					param:{
						code:'DD021025001'
					}
				}
			]
			subOnLoad(_that, e, function(ajaxData) {
				_that.title = e.title
				if(e.canEditorEvaMultiOffId == 'true'){
					_that.canEditorEvaMultiOffId = true
				}
				if(e.busCode){
					_that.busCode = e.busCode
				}
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if (res.data && Object.prototype.toString.call(res.data) ===
								'[object Array]') {
								for (let item of res.data) {
									if (e.title) {
										item.title = e.title
									}
								}
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								if (e.date) {
									_that.minDate = e.date
									changeEditor("title10", "planEnd", 'minDate', e.date, _that.autoformdata)
								}
								if (ajaxData[0].value + '' == '0' ){
									if(_that.formdata.reportEvaCode == '39758') {
										changeEditor('title10', "evaMultiOffId", "type", 'linkbox',_that.autoformdata)
										changeEditor('title10', "evaMultiOffId",{
											"link":'../../public/searchUser/searchUser',
											"linkParam":{
												other:JSON.stringify({
												}),
												defaultDataCache:_that.defaultDataCache,
												isUserPos:true,
												isNew:true,
												cachename:_that.evaOff3CacheKey
											}
										},null,_that.autoformdata)
									}else{
										changeEditor('title10', "evaMultiOffId", "type",'labelbox',_that.autoformdata)
										changeEditor('title10', "maxScore", "type",'labelbox',_that.autoformdata)
									}
								}else{
									_that.canEdit = false
									changeEditor('title10', "evaMultiOffId", "type",'labelbox',_that.autoformdata)
									changeEditor('title10', "maxScore", "type",'labelbox',_that.autoformdata)
								}	
								if(_that.formdata.evaMultiOffId && (ajaxData[0].value + '' == '0')){
									let objArr = []
									let ids = _that.formdata.evaMultiOffId.split(',')
									let names = _that.formdata.evaMultiOffName.split(',')
									let posNames = _that.formdata.evaMultiPosName.split(',')
									for(let index in ids){
										let obj = {
											checked: true,
											id: null,
											instId: null,
											instName: '',
											isUser: true,
											posId: null,
											posInstName:'',
											posName: '',
											username: ''
										}
										let userPosId = ids[index].split('/')
										obj.id = userPosId[0]
										obj.posId = userPosId[1]
										obj.username = names[index]
										obj.posName = posNames[index]
										objArr.push(obj)
									}
									uni.setStorage({
										key:_that.defaultDataCache,
										data:objArr
									})
								}
								
								if (_that.formdata.dutyOffId) {
									let idList = _that.formdata.dutyOffId.split(',')
									let nameList = _that.formdata.dutyOffName.split(',')
									let data = []
									for (let index in idList) {
										let idMixList = idList[index].split('/')
										let nameMixList = nameList[index].split('/')
										let obj = {
											userId: idMixList[0],
											username: nameMixList[0],
											posId: idMixList[1],
											posName: nameMixList[1]
										}
										data.push(obj)
										changeEditor('title10', 'dutyOffId', {
											initSelect: data
										},null,_that.autoformdata)
									}
								}
								_that.initConfig(e)
							}
						}
					});
				}
			},arr)
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: this.evaOff3CacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							uni.setStorage({
								key:_that.defaultDataCache,
								data:d
							})
							var formdata = _that.$refs.autoform.formdata;
							if(d.length){
								let idList = []
								let nameList = []
								let posNameList = []
								for(let index in d){
									idList.push(d[index].id + '/' + d[index].posId)
									nameList.push(d[index].username)
									posNameList.push(d[index].posName)
								}
								
								let idStr = idList.map(e =>e).join(",")
								let nameStr = nameList.map(e =>e).join(",");
								let posNameStr = posNameList.map(e =>e).join(",");
								
								formdata.evaMultiOffId = idStr
								formdata.evaMultiOffName = nameStr
								formdata.evaMultiPosName = posNameStr
							}else{
								_that.$refs.autoform.clear("title_work","evaMultiOffId")
							}
							uni.removeStorage({
								key: _that.evaOff3CacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: this.bearOff3CacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data
							let formdata  = _that.$refs.autoform.formdata
							formdata.dutyOffId = d.map(e=> e.userId +'/'+ e.posId).join(',')
							formdata.dutyOffName = d.map(e=> e.userName).join(',')
							_that.$refs.autoform.changeEditor('title10', 'dutyOffId', {
								initSelect: d
							})
							if(formdata.reportEvaCode == '39758'){
								_that.getMultiEvaUserResponse(formdata.dutyOffId)
							}
							uni.removeStorage({
								key: _that.bearOff3CacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
